﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OpenPowerShellConsole.cs" company="Sitecore A/S">
//   Copyright (C) by Sitecore A/S
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using Sitecore.VisualStudio.ContentTrees;
using Sitecore.VisualStudio.ContentTrees.Items;

namespace Sitecore.VisualStudio.UI.PowerShellConsoles.Commands
{
  using Sitecore.VisualStudio.Commands;
  using Sitecore.VisualStudio.ContentTrees.Commands.Tools;
  using Sitecore.VisualStudio.Data;

  /// <summary>The navigate links.</summary>
  [Command(Submenu = ToolsSubmenu.Name)]
  public class OpenPowerShellConsole : CommandBase
  {
    #region Constructors and Destructors

    /// <summary>
    /// Initializes a new instance of the <see cref="OpenPowerShellConsole"/> class.
    /// </summary>
    public OpenPowerShellConsole()
    {
      this.Text = "Open PowerShell Console";
      this.Group = "PowerShell";
      this.SortingValue = 8510;
    }

    #endregion

    #region Public Methods

    /// <summary>Defines the method that determines whether the command can execute in its current state.</summary>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    /// <returns>true if this command can be executed; otherwise, false.</returns>
    public override bool CanExecute(object parameter)
    {
      var context = parameter as IDatabaseSelectionContext;
      if (context == null)
      {
        return false;
      }

      if (context.DatabaseUri == DatabaseUri.Empty)
      {
        return false;
      }

      return true;
    }

    /// <summary>Defines the method to be called when the command is invoked.</summary>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    public override void Execute(object parameter)
    {
        var context = parameter as IDatabaseSelectionContext;
        if (context == null)
        {
            return;
        }

        var siteContext = parameter as ISiteSelectionContext;
        
        string consoleName = "PowerShell Console";
        if (siteContext != null)
        {
            consoleName += " - " + siteContext.Site.HostName;
        }
        
        Console console = AppHost.OpenDocumentWindow<Console>(consoleName);
        console.Terminal.Background = new SolidColorBrush(Color.FromRgb(1,36,86));

        var itemContext = parameter as IItemSelectionContext;

        console.StartItemPath = context.DatabaseUri.DatabaseName + ":\\";
        if (itemContext != null)
        {
            List<IItem> items = itemContext.Items.ToList();
            if (items.Count > 0)
            {
                var item = items[0] as ItemTreeViewItem;
                if (item != null)
                {
                    string itemPath = item.Item.Path;
                    if (itemPath.StartsWith("/sitecore"))
                    {
                        itemPath = itemPath.Substring(9, itemPath.Length - 9);
                    }
                    console.StartItemPath = context.DatabaseUri.DatabaseName + ":" + itemPath;
                }
            }
        }

        console.DatabaseUri = context.DatabaseUri;
    }

      #endregion
  }
}